//该文件专门用于创建整个应用的路由器
import { createRouter,createWebHistory} from "vue-router";
import axios from "axios";
import {ElMessage} from "element-plus";

//创建并暴露一个路由器
const routes=[
    {
        path:'/login',
        name:'login',
        component:()=> import('../pages/login/Login'),
    },
    {
        path:'/',
        redirect:'/index',
    },
    {
        path:'/index',
        name:'index',
        component:()=> import('../pages/Index'),
        meta:{title:'首页'},
        redirect:'/adminHome',
        children:[
            {
                path: '/adminHome',
                name: 'adminHome',
                component: () => import('../pages/admin/Home'),
                meta: { title: '首页' , type:'管理员',icon:'Menu'},
            },
            {
                path: '/adminHouse',
                name: 'adminHouse',
                component: () => import('../pages/admin/Fee'),
                meta: { title: '住户管理' , type:'管理员',icon:'User'},
                redirect:'/adminHousehold',
                children: [
                    {
                        path: '/adminHousehold',
                        name: 'adminHousehold',
                        component: () => import('../pages/admin/HouseHold'),
                        meta: { title: '住户信息' , type:'管理员',icon:'Document'},
                    }
                ]
            },
            {
                path: '/adminProper',
                name: 'adminProper',
                component: () => import('../pages/admin/Fee'),
                meta: { title: '房产管理',type:'管理员',icon:'OfficeBuilding'},
                children: [
                    {
                        path: '/adminProperty',
                        name: 'adminProperty',
                        component: () => import('../pages/admin/Property'),
                        meta: { title: '房产信息',type:'管理员',icon:'Document'}
                    },
                    {
                        path: '/adminAccount',
                        name: 'adminAccount',
                        component: () => import('../pages/admin/Account'),
                        meta: { title: '系统账号',type:'管理员',icon:'Monitor'}
                    }
                ]
            },
            {
                path: '/adminEquip',
                name: 'adminEquip',
                component: () => import('../pages/admin/Fee'),
                meta: { title: '设备管理' , type:'管理员',icon:'Tools'},
                children: [
                    {
                        path: '/adminEquipment',
                        name: 'adminEquipment',
                        component: () => import('../pages/admin/Equipment'),
                        meta: { title: '设备信息' , type:'管理员',icon:'Document'}
                    }
                ]
            },
            {
                path: '/adminPark',
                name: 'adminPark',
                component: () => import('../pages/admin/Fee'),
                meta: { title: '车位管理' , type:'管理员',icon:'Van'},
                children: [
                    {
                        path: '/adminParking',
                        name: 'adminParking',
                        component: () => import('../pages/admin/Parking'),
                        meta: { title: '车位信息' , type:'管理员',icon:'Document'},
                    }
                ]
            },
            {
                path: '/adminCompla',
                name: 'adminCompla',
                component: () => import('../pages/admin/Fee'),
                meta: { title: '投诉管理' , type:'管理员',icon:'ChatDotRound'},
                children: [
                    {
                        path: '/adminComplain',
                        name: 'adminComplain',
                        component: () => import('../pages/admin/Complain'),
                        meta: { title: '投诉信息' , type:'管理员',icon:'Document'}
                    }
                ]
            },
            {
                path: '/adminRepair',
                name: 'adminRepair',
                component: () => import('../pages/admin/Fee'),
                meta: { title: '报修管理' , type:'管理员',icon:'Suitcase'},
                children: [
                    {
                        path: '/adminRepairs',
                        name: 'adminRepairs',
                        component: () => import('../pages/admin/Repairs'),
                        meta: { title: '报修信息' , type:'管理员',icon:'Document'}
                    }
                ]
            },
            {
                path:'/adminFee',
                name:'adminFee',
                component: () => import('../pages/admin/Fee'),
                meta: { title: '收费管理' , type:'管理员',icon:'Files'},
                children:[
                    {
                        path: '/adminWaterFee',
                        name: 'adminWaterFee',
                        component: () => import('../pages/admin/WaterFee'),
                        meta: { title: '水费账单' , type:'管理员',icon:'ColdDrink'}
                    },
                    {
                        path: '/adminElectricityFee',
                        name: 'adminElectricityFee',
                        component: () => import('../pages/admin/ElectricityFee'),
                        meta: { title: '电费账单' , type:'管理员',icon:'Lightning'}
                    },{
                        path: '/adminGasFee',
                        name: 'adminGasFee',
                        component: () => import('../pages/admin/GasFee'),
                        meta: { title: '气费账单' , type:'管理员',icon:'Odometer'}
                    },{
                        path: '/adminTotalFee',
                        name: 'adminTotalFee',
                        component: () => import('../pages/admin/TotalFee'),
                        meta: { title: '月度账单', type:'管理员',icon:'Tickets' }
                    },
                ]
            },

            {
                path: '/userProper',
                name: 'userProper',
                component: () => import('../pages/user/Fee'),
                meta: { title: '住房信息',type:'普通用户',icon:'OfficeBuilding'},
                children: [
                    {
                        path: '/userProperty',
                        name: 'userProperty',
                        component: () => import('../pages/user/Property'),
                        meta: { title: '基本信息',type:'普通用户',icon:'Document'}
                    },
                    {
                        path: '/userAccount',
                        name: 'userAccount',
                        component: () => import('../pages/user/HouseHold'),
                        meta: { title: '住户信息',type:'普通用户',icon:'User'}
                    }
                ]
            },
            {
                path: '/userCompla',
                name: 'userCompla',
                component: () => import('../pages/user/Fee'),
                meta: { title: '投诉系统' , type:'普通用户',icon:'ChatDotRound'},
                children: [
                    {
                        path: '/userComplain',
                        name: 'userComplain',
                        component: () => import('../pages/user/Complain'),
                        meta: { title: '投诉信息' , type:'普通用户',icon:'Document'}
                    }
                ]
            },
            {
                path: '/userRepair',
                name: 'userRepair',
                component: () => import('../pages/user/Fee'),
                meta: { title: '报修系统' , type:'普通用户',icon:'Suitcase'},
                children: [
                    {
                        path: '/userRepairs',
                        name: 'userRepairs',
                        component: () => import('../pages/user/Repairs'),
                        meta: { title: '报修信息' , type:'普通用户',icon:'Document'}
                    }
                ]
            },
            {
                path:'/userFee',
                name:'userFee',
                component: () => import('../pages/user/Fee'),
                meta: { title: '缴费系统' , type:'普通用户',icon:'Files'},
                children:[
                    {
                        path: '/userWaterFee',
                        name: 'userWaterFee',
                        component: () => import('../pages/user/WaterFee'),
                        meta: { title: '水费查询' , type:'普通用户',icon:'ColdDrink'}
                    },
                    {
                        path: '/userElectricityFee',
                        name: 'userElectricityFee',
                        component: () => import('../pages/user/ElectricityFee'),
                        meta: { title: '电费查询' , type:'普通用户',icon:'Lightning'}
                    },{
                        path: '/userGasFee',
                        name: 'userGasFee',
                        component: () => import('../pages/user/GasFee'),
                        meta: { title: '气费查询' , type:'普通用户',icon:'Odometer'}
                    },{
                        path: '/userTotalFee',
                        name: 'userTotalFee',
                        component: () => import('../pages/user/TotalFee'),
                        meta: { title: '缴费情况', type:'普通用户',icon:'Tickets' }
                    },
                ]
            },
        ]
    }
]
const router = createRouter({
    history:createWebHistory(),
    routes
})
// 路由守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        next()
    } else {
        if(window.sessionStorage.getItem('user')==null){
            ElMessage({message:'请重新登陆！'})
            next('/login')
        }else{
            const user = JSON.parse(window.sessionStorage.getItem('user'))
            if(!user.verify){
                ElMessage({message:'请重新登陆！'})
                next('/login')
            }else{
                axios.get('http://localhost:8085/hasAccount',{
                    params:{
                        accountId:user.verify,   //账号id
                        accountUsername:user.username   //账号用户名
                    }
                }).then(
                    (resp)=>{
                        if(resp.data){
                            next()
                        }
                        else {
                            ElMessage({message:'请重新登陆！'})
                            next('/login')
                        }
                    },
                    ()=>{
                        ElMessage({message:'网络请求失败，请稍后再试！'})
                    }
                )
            }
        }

    }
})

export default router